package com.shenhonghui.photo;

import com.shenhonghui.photo.slice.*;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.IAbilityContinuation;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.agp.components.Component;
import ohos.agp.window.service.WindowManager;
import ohos.bundle.IBundleManager;

import java.util.ArrayList;
import java.util.List;

public class MainAbility extends Ability implements IAbilityContinuation {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setMainRoute(CameraAbilitySlice.class.getName());

        //请求权限
        requestPermission();
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);       //沉浸式状态栏
        WindowManager.getInstance().getTopWindow().get().setStatusBarVisibility(Component.INVISIBLE);     //设置状态栏显示
    }

    //获取权限
    private void requestPermission() {
        String[] permission = {
                "ohos.permission.READ_USER_STORAGE",
                "ohos.permission.WRITE_USER_STORAGE",
                "ohos.permission.DISTRIBUTED_DATASYNC",
                "ohos.permission.CAMERA",
                "ohos.permission.MICROPHONE",
                "ohos.permission.GET_DISTRIBUTED_DEVICE_INFO",
                "ohos.permission.READ_USER_STORAGE",
                "ohos.permission.WRITE_USER_STORAGE",
                "ohos.permission.KEEP_BACKGROUND_RUNNING"};
        List<String> applyPermissions = new ArrayList<>();
        for (String element : permission) {
            if (verifySelfPermission(element) != IBundleManager.PERMISSION_GRANTED) {
                if (canRequestPermission(element)) {
                    applyPermissions.add(element);
                }
            }
        }

        requestPermissionsFromUser(applyPermissions.toArray(new String[0]), 0);
    }

    @Override
    public boolean onStartContinuation() {
        return true;
    }

    @Override
    public boolean onSaveData(IntentParams intentParams) {
        return true;
    }

    @Override
    public boolean onRestoreData(IntentParams intentParams) {
        return true;
    }

    @Override
    public void onCompleteContinuation(int i) {

    }
}